package com.mc.mcgoweb.thread.ThreadJoin;

import java.time.Duration;
import java.time.Instant;

public class ThreadJoin {

    public static void main(String[] args) throws InterruptedException {
        Instant now = Instant.now();

        Thread t1 = new Thread(new JoinRunnable(5_000L, "m1"));
        Thread t2 = new Thread(new JoinRunnable(10_000L, "m2"));
        Thread t3 = new Thread(new JoinRunnable(15_000L, "m3"));

        t1.start();
        t2.start();
        t3.start();

        //等待三个线程执行完毕后再统计耗时时间
        t1.join();
        t2.join();
        t3.join();

        Instant now1 = Instant.now();
        System.out.printf("耗时:" + Duration.between(now, now1).toMillis() + "ms");

    }


}

class JoinRunnable implements Runnable {
    private long executeTime;
    private String threadName;

    public JoinRunnable(long executeTime, String threadName) {
        this.executeTime = executeTime;
        this.threadName = threadName;
    }


    @Override
    public void run() {
        try {
            Thread.sleep(executeTime);
            System.out.println("线程" + threadName + "执行" + executeTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}

